package com.zxl.muban.controller;

import com.zxl.muban.util.R;
import com.zxl.muban.util.UploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: bo-feifei
 * @Date 2021/11/3 19:07
 * @Version 1.0
 */
@RestController
@RequestMapping("upload")
@Api(tags = "上传相关接口")
public class UploadController {

    @PostMapping("/upload")
    @ApiOperation("上传文件")
    public R<String> uploadImg(MultipartFile pic) throws Exception{

        return R.ok(UploadUtil.uploadAli(pic));
    }

    @RequestMapping("/uploadImg_ck")
    public Map<String,Object> uploadImgCk(MultipartFile upload) {
        Map<String,Object> map = new HashMap<>();
        try {
            String pathUrl = UploadUtil.uploadAli(upload);
            map.put("uploaded",1);
            map.put("fileName",upload.getOriginalFilename());
            map.put("url",pathUrl);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("uploaded",0);
            Map<String,String> errorMap = new HashMap<>();
            errorMap.put("message",e.getMessage());
            map.put("error",errorMap);
            return map;
        }
    }
}
